package com.ice_col.guesssongname.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.TextView;

import com.ice_col.guesssongname.R;
import com.ice_col.guesssongname.util.Util;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by Administrator on 2015/10/13 0013.
 */
public class WordsGridView extends GridView implements AdapterView.OnItemClickListener{

    public static final int WORD_NUM=24;
    private MyGridViewAdapter mWordsAdapter=new MyGridViewAdapter();
    private Context mContext;
    private WordSelectListener mCallback;

    private String[] mWord;

    public interface  WordSelectListener{
        void onWordSelected(int position, View view);
    }

    public WordsGridView(Context context) {
        super(context);
        mContext=context;
    }

    public WordsGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext=context;

    }

    public WordsGridView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext=context;

    }

    public void initWords(int[] answer,String songName){
        mWord=new String[WORD_NUM];
        for(int i=0;i<WordsGridView.WORD_NUM;i++){
           mWord[i]=Util.generateChineseCharacter();
        }
        //add the answer to the words
        for (int i=0;i<answer.length;i++) {
            mWord[answer[i]]=""+songName.charAt(i);
        }
        setAdapter(mWordsAdapter);
        setOnItemClickListener(this);
    }
    public void registerListener(WordSelectListener callback){
        mCallback=callback;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        Log.i("tag","item click");
        mCallback.onWordSelected(position,view);

    }

    public String[] getWord() {
        return mWord;
    }

    class MyGridViewAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return mWord.length;
        }

        @Override
        public Object getItem(int position) {
            return mWord[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView holder;
            if(convertView==null){
                convertView= Util.getView(mContext, R.layout.gridview_item);
                holder=((TextView) convertView.findViewById(R.id.gridview_item_button));
                holder.setText(mWord[position]);
                convertView.setTag(holder);
            }else{
                holder=(TextView) convertView.getTag();
            }
            return convertView;
        }
    }
}
